package com.wuzheng.wisgo.sanitation.cloud.service.locate.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wuzheng.wisgo.sanitation.cloud.constant.Constants;
import com.wuzheng.wisgo.sanitation.cloud.constant.ErrorConst;
import com.wuzheng.wisgo.sanitation.cloud.enums.MapTypeEnum;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.pojo.PositionVehicleTempDto;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.locate.ReceiveInfoNew;
import com.wuzheng.wisgo.sanitation.cloud.repository.mapper.locate.ReceiveInfoNewMapper;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.param.PositionParam;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.param.PositionVehicleParam;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.pojo.PositionListDto;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.pojo.PositionDto;
import com.wuzheng.wisgo.sanitation.cloud.model.locate.pojo.PositionVehicleDto;
import com.wuzheng.wisgo.sanitation.cloud.service.person.UserService;
import com.wuzheng.wisgo.sanitation.cloud.service.locate.ReceiveInfoNewService;
import com.wuzheng.wisgo.sanitation.framework.base.service.impl.BaseServiceImpl;
import com.wuzheng.wisgo.sanitation.framework.core.constant.SysRespEnum;
import com.wuzheng.wisgo.sanitation.framework.core.util.ServiceAssert;
import com.wuzheng.wisgo.sanitation.framework.util.BeanUtils;
import com.wuzheng.wisgo.sanitation.framework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：hibon
 * @date：Created in 2019-10-09
 * @description：TBox数据最新数据服务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class ReceiveInfoNewServiceImpl extends BaseServiceImpl<ReceiveInfoNew> implements ReceiveInfoNewService {

    @Resource
    UserService userService;

    @Override
    public PositionListDto selectPositionVoList(PositionParam positionParam) {
        ReceiveInfoNewMapper receiveInfoNewMapper = this.getMapper(ReceiveInfoNewMapper.class);
        List<PositionDto> positionDtoList = receiveInfoNewMapper.selectPositionList(positionParam);

        String mapMark = StringUtils.isBlank(positionParam.getMapMark()) ? MapTypeEnum.GAODE.getTypeId() : positionParam.getMapMark();
        List<PositionDto> result = new ArrayList<>();
        for (int i = 0, len = positionDtoList.size(); i < len; i++) {
            if (mapMark.equals(positionDtoList.get(i).getMapType()) || StringUtils.isBlank(positionDtoList.get(i).getMapType())) {
                result.add(positionDtoList.get(i));
            }
        }

        User user = userService.selectOne(new EntityWrapper<User>().eq("customer_id", positionParam.getOrgId()));

        return new PositionListDto(user.getCustomerName(), result);
    }

    @Override
    public PositionVehicleDto selectPositionVehicleVoInfo(PositionVehicleParam positionVehicleParam) {
        ReceiveInfoNewMapper positionMapper = this.getMapper(ReceiveInfoNewMapper.class);
        PositionVehicleTempDto tempDto = positionMapper.selectPositionVehicleVoInfo(positionVehicleParam);
        ServiceAssert.notNull(tempDto, SysRespEnum.SERVICE_EXCEPTION.getCode(), ErrorConst.Vehicle.NOT_EXIST);

        PositionVehicleDto positionVehicleDto = new PositionVehicleDto();
        BeanUtils.copyPropertiesAllowNull(tempDto, positionVehicleDto);

        switch (tempDto.getVehFuelType()) {
            case Constants.VehicleFuelType.ENERGY:
                positionVehicleDto.setFuelCapacity(tempDto.getSocCapacity());
                break;
            case Constants.VehicleFuelType.HYBRID:
                positionVehicleDto.setFuelCapacity(tempDto.getFuelCapacity().concat("/").concat(tempDto.getSocCapacity()));
                break;
            default:
                break;
        }
        return positionVehicleDto;
    }
}
